/**
 * 清除目录下的空格
 */
 const fs = require('fs')
 const path = require('path')
//  console.log(__dirname)
//  console.log(path.resolve(__dirname))
 let pathStr = path.resolve(__dirname)
 removeSpace(pathStr) // D:\证书\1-证书扫描件

 let reg = /[\t\r\f\n\s]*/g
 // 获得目录
 function removeSpace(path,fname){
    fs.readdir(path,(err,files)=>{
     files.forEach(item=>{
         let pathName = path+'/'+item
         let stat = fs.lstatSync(pathName)
           // 判断是否是文件夹
         if(stat.isDirectory()){
             // 清除文件夹名的空格
             if(item.indexOf(' ')>-1){
               let newName = item.replace(reg,'')
               fs.renameSync(`${path}/${item}`,`${path}/${newName}`)
               fs.writeFileSync('./删除空格的文件.txt',`\r\n文件夹-${path}/${item} vs ${path}/${newName}`,{flag:'a'})
               item = newName 
             }
             removeSpace(`${path}/${item}`,item)
             return
         }
      
         // 重命名
         rename(item,path,fname)
        
     })
    })
 }
 
 function rename(name,path,fname){
  if(name.indexOf(' ')===-1) return
     let newName = name.replace(reg,"")
     fs.renameSync(`${path}/${name}`,`${path}/${newName}`)
     fs.writeFileSync('./删除空格的文件.txt',`\r\n文件名-${path}/${name} vs ${path}/${newName}`,{flag:'a'})
 }